import os
from scipy.io import loadmat, savemat
import numpy as np
import config


NUM_SOURCE = 4
SELECTION = 1


def load_features(data_dir):
	if os.path.isfile(data_dir) and data_dir.endswith(".mat"):
		features, segment_num = _load_features(data_dir)
		num_sample, num_segment, num_feature = features.shape
		print("\tnum_sample = {}, num_segment = {}, num_feature = {}".format(num_sample, num_segment, num_feature))
	else:
		print("Cannot find mat file at \"{}\"...".format(data_dir))

	return features, segment_num


def _load_features(data_dir):
	print("Loading data from \"{}\"...".format(data_dir))
	d = loadmat(data_dir)["features"]
	
	tag1_rssi = d[0]
	tag2_rssi = d[1]
	tag1_phase = d[2]
	tag2_phase = d[3]

	features = []
	sample_num = tag1_rssi.shape[0]
	segment_num = tag1_rssi.shape[1]
	for i in range(sample_num):
		sample_features = []
		for j in range(segment_num):
			tag1_rssi_feature = tag1_rssi[i][j]
			tag2_rssi_feature = tag2_rssi[i][j]
			tag1_phase_feature = tag1_phase[i][j]
			tag2_phase_feature = tag2_phase[i][j]

			## 3 + 3 + 3 + 3 + 500 + 500
			feature = []
			if SELECTION == 1:
				feature.extend(tag1_rssi_feature[0 : config.NUM_STA_FEATURE])
				feature.extend(tag2_rssi_feature[0 : config.NUM_STA_FEATURE])
				feature.extend(tag1_phase_feature[0 : config.NUM_STA_FEATURE])
				feature.extend(tag2_phase_feature[0 : config.NUM_STA_FEATURE])
				feature.extend(tag1_rssi_feature[config.NUM_STA_FEATURE :])
				feature.extend(tag1_phase_feature[config.NUM_STA_FEATURE :])

			sample_features.append(feature)

		features.append(sample_features)

	features = np.array(features)
	return features, segment_num


if __name__ == '__main__':
	load_features("../features/features-air.mat")